package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo5 {
    public static void main(String[] args) {
        //查询
        try (Connection connection = DBUtil.getConnection();){//获取连接 自动关闭连接
            Statement statement = connection.createStatement();//创建执行sql语句的Statement对象
            String sql = "select id,name,salary,title  from teacher";//创建sql语句 查询teacher表中的所有数据
            ResultSet re = statement.executeQuery(sql);//返回结果集
            while (re.next()){//遍历结果集 获取结果集的每一行数据
                int id = re.getInt(1);
                String name = re.getString(2);
                double salary = re.getDouble(3);
                String title = re.getString(4);
                System.out.println(id+" "+name+" "+salary+" "+title);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
